/*
 * @Author: 汪圣哲 wanghist061@163.com
 * @Date: 2024-02-29 14:21:32
 * @LastEditors: 汪圣哲 wanghist061@163.com
 * @LastEditTime: 2024-03-07 16:52:50
 * @FilePath: \my_backstage\src\router\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import Cookie from 'js-cookie'

import Home from '../views/Home.vue'
import User from '../views/User.vue'
import Main from '../views/Main.vue'
import Login from '../views/Login.vue'
import Mall from '../views/Mall.vue'
import Page from '../views/Page.vue'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        component: Main,
        name: 'main',
        redirect: '/home',
        children: [
/*             { path: 'home', component: Home, name: 'home' },
            { path: 'user', component: User, name: 'user' },
            { path: 'mall', component: Mall, name: 'mall' },
            { path: 'page', component: Page, name: 'page' }, */
        ]
    },
    {
        path: '/login',
        component: Login,
        name: 'login'
    }
]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    // 获取token
    const token = Cookie.get('token')

    if (!token && to.name !== 'login') {
        next({ name: 'login' })
    } else if (token && to.name === 'login') {
        next({ name: 'home' })
    } else {
        next()
    }
})

export default router